// stats.js
Page({
  data: {
    studyProgress: {
      totalWords: 0,
      learnedWords: 0,
      todayLearned: 0,
      streak: 0,
      lastStudyDate: null
    },
    wordList: [],
    difficultyStats: {
      easy: 0,
      medium: 0,
      hard: 0
    },
    weeklyProgress: [],
    monthlyProgress: []
  },

  onLoad() {
    this.loadStats()
  },

  onShow() {
    this.loadStats()
  },

  // 加载统计数据
  loadStats() {
    const wordData = wx.getStorageSync('wordData') || []
    const progress = wx.getStorageSync('studyProgress') || {}
    
    // 计算难度统计
    const difficultyStats = {
      easy: wordData.filter(word => word.difficulty === 1 && word.learned).length,
      medium: wordData.filter(word => word.difficulty === 2 && word.learned).length,
      hard: wordData.filter(word => word.difficulty === 3 && word.learned).length
    }

    // 生成周进度数据
    const weeklyProgress = this.generateWeeklyProgress()
    
    // 生成月进度数据
    const monthlyProgress = this.generateMonthlyProgress()

    this.setData({
      wordList: wordData,
      studyProgress: progress,
      difficultyStats,
      weeklyProgress,
      monthlyProgress
    })
  },

  // 生成周进度数据
  generateWeeklyProgress() {
    const weekData = []
    const today = new Date()
    
    for (let i = 6; i >= 0; i--) {
      const date = new Date(today)
      date.setDate(date.getDate() - i)
      const dateStr = date.toDateString()
      
      // 这里可以扩展为从本地存储读取每日学习数据
      weekData.push({
        date: dateStr,
        count: Math.floor(Math.random() * 10) // 模拟数据
      })
    }
    
    return weekData
  },

  // 生成月进度数据
  generateMonthlyProgress() {
    const monthData = []
    const today = new Date()
    const daysInMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0).getDate()
    
    for (let i = 1; i <= daysInMonth; i++) {
      // 这里可以扩展为从本地存储读取每日学习数据
      monthData.push({
        day: i,
        count: Math.floor(Math.random() * 15) // 模拟数据
      })
    }
    
    return monthData
  },

  // 重置学习进度
  resetProgress() {
    wx.showModal({
      title: '确认重置',
      content: '确定要重置所有学习进度吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          // 重置单词数据
          const wordData = wx.getStorageSync('wordData') || []
          const resetWordData = wordData.map(word => ({
            ...word,
            learned: false,
            reviewCount: 0
          }))
          wx.setStorageSync('wordData', resetWordData)
          
          // 重置学习进度
          wx.setStorageSync('studyProgress', {
            totalWords: 0,
            learnedWords: 0,
            todayLearned: 0,
            streak: 0,
            lastStudyDate: null
          })
          
          wx.showToast({
            title: '重置成功',
            icon: 'success'
          })
          
          this.loadStats()
        }
      }
    })
  },

  // 导出学习数据
  exportData() {
    const wordData = wx.getStorageSync('wordData') || []
    const progress = wx.getStorageSync('studyProgress') || {}
    
    const exportData = {
      wordData,
      studyProgress: progress,
      exportDate: new Date().toISOString()
    }
    
    // 在实际应用中，这里可以将数据发送到服务器或保存到本地文件
    console.log('导出数据:', exportData)
    
    wx.showToast({
      title: '数据已导出',
      icon: 'success'
    })
  },

  // 返回主页
  goBack() {
    wx.navigateBack()
  }
})
